﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace A1
{
    class Walls
    {
        Ball b;
        Rectangle rec;
        int corner;
        int gameWidth;
        int gameHeight;

        /// <summary>
        /// Constructor for walls.
        /// </summary>
        /// <param name="b">Ball of the game.</param>
        /// <param name="c">Which corner it's in, clockwise from top left. 0-3</param>
        /// <param name="gw">Game Width</param>
        /// <param name="gh">Game Height</param>
        public Walls(Ball b, int c, int gw, int gh)
        {
            this.b = b; corner = c;
            if (corner == 0)
            { rec = new Rectangle(0, 0, 30, 30); }
            if (corner == 1)
            { rec = new Rectangle(gw - 30, 0, 30, 30); }
            if (corner == 2)
            { rec = new Rectangle(gw - 30, gh - 30, 30, 30); }
            if (corner == 3)
            { rec = new Rectangle(0, gh - 30, 30, 30); }
            gameWidth = gw; gameHeight = gh;
        }

        /// <summary>
        /// If the ball collides with the wall, it tries to decide which 
        /// side it hit and reflect off that.
        /// </summary>
        public void WallUpdate()
        {
            if (rec.Intersects(b.Rec))
            {
                if (corner == 0)
                {
                    if (b.Rec.X > b.Rec.Y)
                    { b.SpeedX *= -1; }
                    else if (b.Rec.Y > b.Rec.X)
                    { b.SpeedY *= -1; }
                    else
                    {
                        b.SpeedY *= -1;
                        b.SpeedX *= -1;
                    }
                }
                else if (corner == 1)
                {
                    if ((b.Rec.X - gameWidth) + 30 > b.Rec.Y)
                    { b.SpeedX *= -1; }
                    else if ((b.Rec.X - gameWidth) + 30 < b.Rec.Y)
                    { b.SpeedY *= -1; }
                    else
                    {
                        b.SpeedY *= -1;
                        b.SpeedX *= -1;
                    }
                }
                else if (corner == 2)
                {
                    if (b.Rec.X > b.Rec.Y)
                    { b.SpeedY *= -1; }
                    else if (b.Rec.X < b.Rec.Y)
                    { b.SpeedX *= -1; }
                    else
                    {
                        b.SpeedY *= -1;
                        b.SpeedX *= -1;
                    }
                }
                else if (corner == 3)
                {
                    if ((b.Rec.Y - gameHeight) + 30 < b.Rec.X)
                    { b.SpeedX *= -1; }
                    else if ((b.Rec.Y - gameHeight) + 30 > b.Rec.X)
                    { b.SpeedY *= -1; }
                    else
                    {
                        b.SpeedY *= -1;
                        b.SpeedX *= -1;
                    }
                }
            }
        }
    }
}
